package com.tich.blog.listener;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.system.UserInfo;
import com.tich.blog.domain.entity.Blog;
import com.tich.blog.domain.entity.BlogDeleted;
import com.tich.blog.service.IBlogDeletedService;
import com.tich.constant.RabbitMqConstant;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class BlogDeleteListener {

    @Autowired
    IBlogDeletedService blogDeletedService;

    @RabbitListener(bindings = @QueueBinding(
            value =  @Queue(name = RabbitMqConstant.BLOG_DELETE_QUEUE),
            exchange = @Exchange(name = RabbitMqConstant.BLOG_DIRECT,type = ExchangeTypes.DIRECT),
            key = RabbitMqConstant.BLOG_DELETE_KEY
        )
    )
    public void deletedListener(Blog blog) {
        if(blog != null) {
            blogDeletedService.save(BeanUtil.copyProperties(blog, BlogDeleted.class)
                    .setCreateTime(LocalDateTime.now()));
        }
    }

}
